#!/usr/bin/env python3
"""
findfs MCP Server

面向软件：findfs
所有 MCP tool 的列表：
- findfs_by_uuid: 根据 UUID 查找文件系统设备
  参数：uuid (字符串，文件系统的 UUID)
  返回：{"success": bool, "command": str, "exit_code": int, "stdout": str, "stderr": str}
"""

from mcp.server.fastmcp import FastMCP, Context
import subprocess
import json

mcp = FastMCP("findfs MCP Server")

@mcp.tool()
def findfs_by_uuid(uuid: str, context: Context) -> dict:
    """
    根据 UUID 查找文件系统设备
    
    参数:
        uuid: 文件系统的 UUID 标识符
        
    返回:
        JSON 结构:
        {
            "success": bool,      # 命令是否成功执行
            "command": str,       # 执行的完整命令
            "exit_code": int,     # 命令退出码
            "stdout": str,        # 命令标准输出
            "stderr": str         # 命令标准错误
        }
    """
    try:
        command = f'findfs UUID="{uuid}"'
        result = subprocess.run(command, shell=True, capture_output=True, text=True, timeout=30)
        
        return {
            "success": result.returncode == 0,
            "command": command,
            "exit_code": result.returncode,
            "stdout": result.stdout.strip(),
            "stderr": result.stderr.strip()
        }
    except subprocess.TimeoutExpired:
        return {
            "success": False,
            "command": command,
            "exit_code": -1,
            "stdout": "",
            "stderr": "Command timed out after 30 seconds"
        }
    except Exception as e:
        return {
            "success": False,
            "command": command,
            "exit_code": -1,
            "stdout": "",
            "stderr": f"Error executing command: {str(e)}"
        }

if __name__ == "__main__":
    mcp.run()